package cn.com.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiT_member;
import cn.com.api.entity.ApiTmember;
import cn.com.api.entity.ApiU_message;
import cn.com.api.entity.ApiU_see_his;
import cn.com.api.entity.ApiU_user_friend;
import cn.com.api.entity.ApiU_user_friend_group;
import cn.com.api.entity.ApiU_user_friend_group_detail;
import cn.com.api.entity.ApiU_user_friend_setting;
import cn.com.api.entity.MemberObject;
import cn.com.api.service.ApiT_memberService;
import cn.com.api.service.ApiU_messageService;
import cn.com.api.service.ApiU_see_hisService;
import cn.com.api.service.ApiU_user_friendService;
import cn.com.api.service.ApiU_user_friend_groupService;
import cn.com.api.service.ApiU_user_friend_group_detailService;
import cn.com.api.service.ApiU_user_friend_settingService;
import cn.com.api.service.CheckService;
import cn.com.util.JsonUtil;
import cn.com.util.PageInfo;
import cn.com.util.Util;
import cn.com.util.Utils;

@Controller
@RequestMapping(Utils.SYS_API + "user_friend/")
public class ApiU_user_friendController {

	@Autowired
	private ApiU_user_friendService apiU_user_friendService;
	@Autowired
	private ApiU_user_friend_groupService apiU_user_friend_groupService;
	@Autowired
	private ApiU_user_friend_group_detailService apiU_user_friend_group_detailService;
	@Autowired
	private ApiU_messageService apiU_messageService;
	@Autowired
	private ApiT_memberService apiT_memberService;
	@Autowired
	private CheckService checkService;
	@Autowired
	private ApiU_user_friend_settingService apiU_user_friend_settingService;

	private static Logger logger = Logger.getLogger(ApiU_user_friendController.class);

	/**
	 * 加好友请求
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "addfriendRequest")
	public void addfriendRequest(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String friend_id, String msg) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(user_id, token, map)) {
				ApiU_user_friend ufriend = this.apiU_user_friendService.getUserFriend(user_id, friend_id);
				if (Util.isNull(ufriend)) {
					ApiT_member active_member = this.apiT_memberService.findT_memberById(Integer.valueOf(user_id));
					ApiT_member passive_member = this.apiT_memberService.findT_memberById(Integer.valueOf(friend_id));
					ApiU_user_friend user_friend = new ApiU_user_friend();
					user_friend.setFriend_id(friend_id);
					user_friend.setF_rename(passive_member.getC_nick());
					user_friend.setUser_id(user_id);
					user_friend.setU_rename(active_member.getC_nick());
					user_friend.setAdd_time(Util.CurrentTime());
					user_friend.setIs_deal(0);
					user_friend.setStatus(0);
					user_friend.setMsg(msg);
					Integer ret = this.apiU_user_friendService.U_user_friendInsertion(user_friend);
					if (ret == 1) {
						// 通知对方加好友请求
						ApiU_message message = new ApiU_message();
						message.setConn_id(user_friend.getId());
						message.setType(1);
						message.setAdd_time(Util.CurrentTime());
						message.setIs_read(0);
						message.setUser_id(friend_id);
						message.setTitle("请求加为好友");
						message.setContent(msg);
						this.apiU_messageService.U_messageInsertion(message);
						map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
						map.put(Utils.MESSAGE, "操作成功");
					} else {
						map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
						map.put(Utils.MESSAGE, "请求加为好友失败！");
					}
				} else {
					// 好友关系状态 0申请中， 1已通过， 2已拒绝, 3黑名单
					if (ufriend.getStatus() == 0 || ufriend.getStatus() == 2) {
						if (ufriend.getUser_id().equals(friend_id)) {
							String uid = ufriend.getUser_id();
							String uname = ufriend.getU_rename();

							String fid = ufriend.getFriend_id();
							String fname = ufriend.getF_rename();

							ufriend.setFriend_id(uid);
							ufriend.setF_rename(uname);
							ufriend.setUser_id(fid);
							ufriend.setU_rename(fname);

						}
						String currentTime = Util.CurrentTime();
						ufriend.setAdd_time(currentTime);
						ufriend.setStatus(0);
						ufriend.setIs_deal(0);
						ufriend.setMsg(msg);
						this.apiU_user_friendService.updateU_user_friend(ufriend);
						// 跟新消息记录
						map.clear();
						map.put("conn_id", ufriend.getId());
						map.put("type", 1);
						ApiU_message message = apiU_messageService.findMessagesByMap(map);
						message.setAdd_time(currentTime);
						message.setIs_read(0);
						apiU_messageService.updateU_message(message);
						map.clear();
						map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
						map.put(Utils.MESSAGE, "操作成功");
					} else if (ufriend.getStatus() == 1) {
						map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
						map.put(Utils.MESSAGE, "已是好友");
					} else {
						map.put(Utils.RESULT, Utils.RESULT_RC_NO_DATA);
						map.put(Utils.MESSAGE, "不可添加此用户为好友！");
					}
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 加好友请求--同意1/忽略2 flag
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "acceptfriendRequest")
	public void acceptfriendRequest(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String conn_id, String flag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", conn_id);
		try {
			if (checkService.checkToken(user_id, token, map)) {
				map.clear();
				map = apiU_user_friendService.acceptfriendRequest(user_id, conn_id, flag);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除好友关系
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "delFriendRelation")
	public void delFriendRelation(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String friend_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(user_id, token, map)) {
				map.clear();
				map = apiU_user_friendService.delFriendRelation(user_id, friend_id);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 检查好友状态
	 * 
	 * @param request
	 * @param response
	 * @return 0可加好友， 1申请中， 2已是好友， 3已拒绝， 4黑名单
	 * @throws Exception
	 */
	@RequestMapping(value = "checkfriendState")
	public void checkfriendState(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String friend_id) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();

		try {
			if (checkService.checkToken(user_id, token, map)) {
				Integer retState = this.apiU_user_friendService.checkUserFriendState(user_id, friend_id);
				params.put("friendState", retState);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功！");
				map.put(Utils.DATAS, params);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
			map.put(Utils.DATAS, null);
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 检查好友状态V2
	 * 
	 * @param request
	 * @param response
	 * @return 0可加好友， 1申请中， 2已是好友， 3已拒绝， 4黑名单
	 * @throws Exception
	 */
	@RequestMapping(value = "getfriendState")
	public void getfriendState(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String friend_id) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();

		try {
			if (checkService.checkToken(user_id, token, map)) {
				ApiU_user_friend user_friend = this.apiU_user_friendService.getUserFriendState(user_id, friend_id);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功！");
				map.put(Utils.DATAS, user_friend);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
			map.put(Utils.DATAS, null);
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 我的好友/黑名单列表 Status=1好友，Status=2企业主好友， Status=3黑名单
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "myFriends")
	public void myFriends(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String status, PageInfo pageinfo) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("user_id", user_id);
		params.put("status", status);

		try {
			if (checkService.checkToken(user_id, token, map)) {

				List<ApiU_user_friend> ufriends = this.apiU_user_friendService.findU_user_friendByMap(params, pageinfo);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成！");
				map.put(Utils.DATAS, ufriends);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 好友/黑名单搜索
	 * 
	 * @param request
	 * @param response
	 * @param keyword关键字
	 * @param status=1好友，
	 *            status=3黑名单
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getFriendsByWord")
	public void getFriendsByWord(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String keyword, String status, PageInfo page) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();

		try {
			if (checkService.checkToken(user_id, token, map)) {
				params.put("user_id", user_id);
				params.put("status", status);
				params.put("keyword", keyword);

				List<ApiU_user_friend> ufriends = this.apiU_user_friendService.findU_user_friendByMap(params, page);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成！");
				map.put(Utils.DATAS, ufriends);
				map.put(Utils.Page, page);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
			map.put(Utils.DATAS, null);
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 创建好友分组
	 * 
	 * @param request
	 * @param response
	 * @param group_name分组名
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "createFriendGroup")
	public void createFriendGroup(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String group_name) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();

		try {
			if (checkService.checkToken(user_id, token, map)) {
				if (!group_name.equals("我的消息")) {
					ApiU_user_friend_group f_group = this.apiU_user_friend_groupService
							.getMaxSortUserFriendGroup(user_id);
					Integer sort = f_group.getSort();
					ApiU_user_friend_group new_group = new ApiU_user_friend_group();
					new_group.setAdd_time(Util.CurrentTime());
					new_group.setCreate_id(user_id);
					new_group.setGroup_name(group_name);
					new_group.setSort(sort + 1);
					Integer ret = this.apiU_user_friend_groupService.U_user_friend_groupInsertion(new_group);

					if (ret != 0) {
						map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
						map.put(Utils.MESSAGE, "操作完成！");
					} else {
						map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
						map.put(Utils.MESSAGE, "操作失败！");
					}
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
					map.put(Utils.MESSAGE, "我的消息为系统默认分组，不可创建！");
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
			map.put(Utils.DATAS, null);
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 搜索好友加入分组
	 * 
	 * @param request
	 * @param response
	 * @param keyword关键字
	 * @param status=1好友
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getFriendsByGroupID")
	public void getFriendsByGroupID(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String group_id, PageInfo page) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();

		try {
			if (checkService.checkToken(user_id, token, map)) {
				params.put("user_id", user_id);
				params.put("status", "1");
				params.put("group_id", group_id);
				params.put("type", '0');

				List<ApiU_user_friend> ufriends = this.apiU_user_friendService.findU_user_friendByMap(params, page);
				if (ufriends != null && ufriends.size() > 0) {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作完成！");
					map.put(Utils.DATAS, ufriends);
					map.put(Utils.Page, page);
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_NO_DATA);
					map.put(Utils.MESSAGE, "无信息！");
					map.put(Utils.DATAS, null);
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
			map.put(Utils.DATAS, null);
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 添加分组成员
	 * 
	 * @param request
	 * @param response
	 * @param group_id分组ID
	 * @param friend_ids
	 *            好友ID，多个用，分隔
	 * @param type
	 *            移动的类型 0-移动人；1-移动圈子
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "addGroupMembers")
	public void addGroupMembers(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String curr_group_id, String to_group_id, String friend_ids, String type) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(user_id, token, map)) {
				if (this.apiU_user_friend_group_detailService.checkUserExistGroup(friend_ids, to_group_id)) {
					// ApiU_user_friend_group_detail detail =
					// this.apiU_user_friend_group_detailService.getFriendGroupDetail(curr_group_id,
					// );
					// ApiU_user_friend_group f_group =
					// this.apiU_user_friend_groupService.getUserFriendGroup(user_id,
					// "我的消息");
					this.apiU_user_friend_group_detailService.addMember2Group(curr_group_id, to_group_id, friend_ids,
							type);

					// String f_ids[] = friend_ids.split(",");
					// for(String f_id : f_ids)
					// {
					//// ApiU_user_friend_group_detail f_group_detail = new
					// ApiU_user_friend_group_detail();
					//// f_group_detail.setFriend_group_id(group_id);
					//// f_group_detail.setAdd_time(Util.CurrentTime());
					//// f_group_detail.setFriend_id(f_id);
					//// this.apiU_user_friend_group_detailService.U_user_friend_group_detailInsertion(f_group_detail);
					//
					// }
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作完成！");
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
					map.put(Utils.MESSAGE, "分组中用户已存在，不可重复添加！");
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
			map.put(Utils.DATAS, null);
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 获取分组成员信息
	 * 
	 * @param request
	 * @param response
	 * @param group_id分组ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getGroupMembers")
	public void getGroupMembers(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String group_id, PageInfo page) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();

		try {
			if (checkService.checkToken(user_id, token, map)) {
				params.put("user_id", user_id);
				params.put("group_id", group_id);

				List<ApiU_user_friend_group_detail> group_members = this.apiU_user_friend_group_detailService
						.getfriend_group_details(params, page);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成！");
				map.put(Utils.DATAS, group_members);
				map.put(Utils.Page, page);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
			map.put(Utils.DATAS, null);
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除分组成员
	 * 
	 * @param request
	 * @param response
	 * @param group_detail_ids
	 *            好友分组明细表ID，多个用，分隔
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "delGroupMembers")
	public void delGroupMembers(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String group_detail_ids) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();

		try {
			if (checkService.checkToken(user_id, token, map)) {
				ApiU_user_friend_group f_group = this.apiU_user_friend_groupService.getUserFriendGroup(user_id, "我的消息");
				// this.apiU_user_friend_group_detailService.addMember2Group(f_group.getId().toString(),
				// group_id, friend_ids);

				String gd_ids[] = group_detail_ids.split(",");
				for (String gd_id : gd_ids) {
					ApiU_user_friend_group_detail detail = this.apiU_user_friend_group_detailService
							.findU_user_friend_group_detailById(Integer.valueOf(gd_id));
					detail.setFriend_group_id(f_group.getId().toString());
					this.apiU_user_friend_group_detailService.updateU_user_friend_group_detail(detail);
				}
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
			map.put(Utils.DATAS, null);
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 获取分组
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getFriendGroups")
	public void getFriendGroups(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			PageInfo page) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();

		try {
			if (checkService.checkToken(user_id, token, map)) {
				params.put("user_id", user_id);

				List<ApiU_user_friend_group> friend_groups = this.apiU_user_friend_groupService.getfriend_groups(params,
						page);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成！");
				map.put(Utils.DATAS, friend_groups);
				map.put(Utils.Page, page);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
			map.put(Utils.DATAS, null);
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除分组
	 * 
	 * @param request
	 * @param response
	 * @param group_id
	 *            好友分组ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "delFriendGroup")
	public void delFriendGroup(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String group_id) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();

		try {
			if (checkService.checkToken(user_id, token, map)) {

				ApiU_user_friend_group group = apiU_user_friend_groupService
						.findU_user_friend_groupById(Integer.parseInt(group_id));
				if (!Util.isNull(group)) {
					if (!group.getGroup_name().equals("我的消息")) {
						ApiU_user_friend_group f_group = this.apiU_user_friend_groupService.getUserFriendGroup(user_id,
								"我的消息");
						Integer ret = this.apiU_user_friend_groupService.delfriend_group(group_id,
								f_group.getId().toString());
						if (ret > 0) {
							map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
							map.put(Utils.MESSAGE, "操作完成！");
						} else {
							map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
							map.put(Utils.MESSAGE, "删除失败！");
						}
					} else {
						map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
						map.put(Utils.MESSAGE, "我的消息不可删除！");
					}
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
					map.put(Utils.MESSAGE, "分组不存在！");
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
			map.put(Utils.DATAS, null);
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 查找通讯录(看通讯录中的手机号在首媒平台的注册信息)
	 * 
	 * @param request
	 * @param response
	 * @param communication_list
	 *            手机通讯录手机号List，以，分隔
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "searchCommunicationList")
	public void searchCommunicationList(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String communication_list) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(user_id, token, map)) {

				List<MemberObject> member_list = this.apiU_user_friend_groupService
						.searchUserFromCommunicationList(communication_list);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成！");
				map.put(Utils.DATAS, member_list);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 添加到黑名单
	 * 
	 * @param request
	 * @param response
	 * @param to_uid
	 *            好友ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "addBlackList")
	public void addBlackList(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String to_uid) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(user_id, token, map)) {
				params.put("user_id", user_id);
				// params.put("status", 1);
				params.put("to_uid", to_uid);
				PageInfo page = new PageInfo();
				page.setCurrentPage(1);
				page.setShowCount(10);
				ApiU_user_friend user_friend = this.apiU_user_friendService.findUser_friend_Record(params);
				if (Util.isNull(user_friend)) {
					map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
					map.put(Utils.MESSAGE, "好友不存在，请确认！");
				} else {
					// 谁把谁加入了黑名单
					// Ext1:谁把谁加入了黑名单：谁把谁加入了黑名单：user_id, friend_id, both两人都加了
					if (user_friend.getUser_id().equals(user_id))// 我加了friend黑名单
					{
						if (user_friend.getExt1().equals(user_friend.getFriend_id())) {
							user_friend.setExt1("both");
						} else {
							user_friend.setExt1(user_id);
						}
					} else {// friend加了我黑名单
						if (!user_friend.getExt1().equals(user_id)) {
							user_friend.setExt1("both");
						} else {
							user_friend.setExt1(user_friend.getFriend_id());
						}
					}
					user_friend.setStatus(3);
					user_friend.setUpdate_time(Util.CurrentTime());
					this.apiU_user_friendService.updateU_user_friend(user_friend);
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作完成！");
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除黑名单
	 * 
	 * @param request
	 * @param response
	 * @param to_uid
	 *            好友ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "delBlackList")
	public void delBlackList(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String to_uid) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(user_id, token, map)) {
				params.put("user_id", user_id);
				params.put("status", 3);
				params.put("to_uid", to_uid);
				ApiU_user_friend user_friend = this.apiU_user_friendService.findUser_friend_Record(params);
				if (Util.isNull(user_friend)) {
					map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
					map.put(Utils.MESSAGE, "黑名单成员不存在，请确认！");
				} else {
					// 谁把谁加入了黑名单
					// Ext1:谁把谁加入了黑名单：谁把谁加入了黑名单：user_id, friend_id, both两人都加了
					if (user_friend.getExt1().equals("both")) {
						user_friend.setExt1(to_uid);
						user_friend.setUpdate_time(Util.CurrentTime());
						this.apiU_user_friendService.updateU_user_friend(user_friend);
					} else {// friend加了我黑名单
						if (user_friend.getExt1().equals(user_id)) {
							user_friend.setExt1("");
							user_friend.setStatus(1);
							user_friend.setUpdate_time(Util.CurrentTime());
							this.apiU_user_friendService.updateU_user_friend(user_friend);
						}
						// else {
						// user_friend.setExt1(user_friend.getFriend_id());
						// }
					}

					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作完成！");
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 黑名单列表
	 * 
	 * @param request
	 * @param response
	 * @param to_uid
	 *            好友ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getUserBlackList")
	public void getUserBlackList(HttpServletRequest request, HttpServletResponse response, String user_id, String token)
			throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(user_id, token, map)) {
				params.put("user_id", user_id);
				params.put("status", 3);
				List<ApiU_user_friend> user_friend = this.apiU_user_friendService.findUser_friend_Black_List(params);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成！");
				map.put(Utils.DATAS, user_friend);

			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 查询对好友的设置信息（包含我是否想阅读好友动态的）
	 * 
	 * @param request
	 * @param response
	 * @param to_uid
	 *            好友ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getFriendSetting")
	public void getFriendSetting(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String friend_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(user_id, token, map)) {
				map.clear();
				ApiU_user_friend_setting setting = apiU_user_friend_settingService.getFriendSetting(user_id, friend_id);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成！");
				map.put(Utils.DATAS, setting);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 设置是否看好友的动态
	 * 
	 * @param request
	 * @param response
	 * @param to_uid
	 *            好友ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "setisreaddynamic")
	public void setisreaddynamic(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String friend_id, String is_readdynamic) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(user_id, token, map)) {
				map.clear();
				apiU_user_friend_settingService.setisreaddynamic(user_id, friend_id, is_readdynamic);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 修改对好友的设置信息
	 * 
	 * @param request
	 * @param response
	 * @param to_uid
	 *            好友ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "updateFriendSetting")
	public void updateFriendSetting(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, ApiU_user_friend_setting setting) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(user_id, token, map)) {
				map.clear();
				map = apiU_user_friend_settingService.updateUserFriendSetting(setting);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
}
